package org.jimm.app;

import org.jimm.protocol.Protocol;
import org.jimm.protocol.StatusInfo;
import org.jimm.service.Preferences;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * @author r.u.s.h.a.n.
 */
public class StatusActivity extends Activity {

    protected static final String PROTOCOL_ID = "protocol_id";
    protected static final String TYPE = "type";
    private Protocol protocol;
    private StatusAdapter statusAdapter;
    private Preferences p = JimmApplication.getInstance().getPreferences();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status_list);
        Intent intent = getIntent();
        final ListView statusList = (ListView) findViewById(R.id.status_list);
        final String id = intent.getStringExtra(PROTOCOL_ID);
        final String type = intent.getStringExtra(TYPE);

        if (type.equals("status")) {
            protocol = JimmApplication.getInstance().getAccountsManager().getProtocol(Integer.valueOf(id));
            setTitle(protocol.getNick());
            statusAdapter = new StatusAdapter(StatusActivity.this, StatusAdapter.ADAPTER_STATUS);
        } else if (type.equals("global_status")) {
            setTitle(R.string.status_global);
            statusAdapter = new StatusAdapter(StatusActivity.this, StatusAdapter.ADAPTER_GLOBALSTATUS);
        } else if (type.equals("private_status")) {
            setTitle(R.string.private_status);
            statusAdapter = new StatusAdapter(StatusActivity.this, StatusAdapter.ADAPTER_PRIVATESTATUS);
        }
        statusList.setAdapter(statusAdapter);

        statusList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                if (type.equals("global_status")) {
                    if (position != StatusInfo.STATUS_OFFLINE) {   // todo ???
                        JimmApplication.getInstance().getAccountsManager().setStatus(position, "");
                        JimmApplication.getInstance().getAccountsManager().connectAll();
                    } else
                        JimmApplication.getInstance().getAccountsManager().disconnectAll();

                    p.statusIndex = position;
                    p.save();
                } else if (type.equals("status")) {
                    if (position != StatusInfo.STATUS_OFFLINE) {
                        protocol.setOnlineStatus(position, "");

                        if (!protocol.isConnected()) protocol.connect();
                    } else
                        protocol.disconnect();

                    p.statusIndex = position;
                    p.save();
                } else {
                    JimmApplication.getInstance().getAccountsManager().setPrivateStatus(position);
                    p.privateStatus = position;
                    p.save();
                }

                finish();
            }
        });
    }

}